package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.*;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.enums.OrderStatus;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.domain.mall.MallCouponUser;
import com.tencreat.domain.mall.MallStoreOrderGoods;
import com.tencreat.domain.mall.dto.*;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.service.account.IMallStoreService;
import com.tencreat.service.mall.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Administrator
 */
//@Api(tags = {"4-APP店铺商品接口"})
//@RequestMapping("/api/StoreGoods")
//@RestController
@RequiredArgsConstructor
public class MallAppStoreGoodsController extends BaseController {

//    private final IMallGoodsService mallGoodsService;//商品基本信息
//    private final IMallOrderService mallOrderService;//商城订单
    private final IMallStoreOrderService mallStoreOrderService;//店铺订单
    private final IMallCommentService mallCommentService;//评论
//    private final IMallOrderGoodsService mallOrderGoodsService;//订单商品
    private final IMallStoreOrderGoodsService storeOrderGoodsService;//订单商品
    private final IMallCouponService mallCouponService;//优惠券信息及规则
    private final IMallSetmealNormsService setmealNormsService;//店铺商品规格参数

    private final IMallStoreGoodsService mallStoreGoodsService;//店铺商品基本信息

    private final IMallStoreService mallStoreService;//店铺

    /**
     * 新增评论
     */
    @ApiOperation(value = "新增评论")
    @PostMapping(value = "/comment/add")
    public HttpResult add(@RequestBody MallCommentDTO mallCommentDTO) {
        String orderSn = mallCommentDTO.getOrderSn();
        mallCommentDTO.setOrderGoodsSn(mallCommentDTO.getOrderSn());
        // 查询订单信息
        MallStoreOrderGoods goods = storeOrderGoodsService.lambdaQuery().eq(MallStoreOrderGoods::getOrderSn, mallCommentDTO.getOrderSn())
                .eq(MallStoreOrderGoods::getGoodsId, mallCommentDTO.getGoodsId()).eq(MallStoreOrderGoods::getNormsId, mallCommentDTO.getNormsId()).one();
        if (goods != null && goods.getOrderGoodsSn() != null) {
            mallCommentDTO.setOrderGoodsSn(goods.getOrderGoodsSn());
        }

        mallCommentService.insertMallComment(mallCommentDTO);
        //修改评论修改订单商品状态
        storeOrderGoodsService.updateOrderTypeByGoodsNorms(orderSn, mallCommentDTO.getGoodsId(), mallCommentDTO.getNormsId(), OrderStatus.OK.getValue(), null);
        return HttpResult.ok();
    }


    @ApiOperation(value = "店铺商品列表 集成 搜索 推荐商品")
    @PostMapping("/list")
    @Deprecated
    @ApiIgnore
    public HttpResult<IPage<MallStoreGoodsListVO>> list(@RequestBody MallStoreGoodsSearchDTO mallStoreGoodsSearchDTO) {

        return HttpResult.ok(mallStoreGoodsService.apiList(mallStoreGoodsSearchDTO));
    }

    @ApiOperation(value = "店铺商品详情")
    @PostMapping("/good_info")
    public HttpResult<MallStoreGoodsDetailsVO> goodInfo(@RequestBody ReqIdParam idParam) {
        return HttpResult.ok(mallStoreGoodsService.goodInfoById(idParam));
    }


    /**
     * 描述信息  拨号接口
     * @Author yaoruiqi
     * @Date: 2023/10/11 02:49
     */
    @ApiOperation(value = "拨号接口")
    @PostMapping("/call")
    public   HttpResult<String> call(@PathVariable Long id){

        return mallStoreService.getMobileById(id);
    }

    @ApiOperation(value = "预下单")
    @PostMapping("/budget")
    public HttpResult<ResStoreOrderMallBudgetVO> budget(@RequestBody StoreOrderMallBudgetDTO storeOrderMallBudgetDTO) {
        return mallStoreOrderService.budget(storeOrderMallBudgetDTO, getLoginCheck());
    }

    /**
     * 店铺商品提交订单
     *
     * @return ResOrderMallSubmit
     * @author huangwei
     * @date 2021年9月6日
     */
    @PostMapping("/submit")
    @ApiOperation(value = "店铺商品提交订单")
    public HttpResult<ResOrderMallSubmitVO> submit(@Valid @RequestBody OrderStoreMallSubmitDTO order) {
        return mallStoreOrderService.submit(order, getLoginCheck());
    }

    /**
     * 订单列表
     *
     * @Author wangbingcheng
     * @Date 2021年09月13日
     * @Return
     */
    @PostMapping("/order_list")
    @ApiOperation(value = "订单列表")
    public HttpResult<IPage<ResStoreOrderGoodsVO>> orderList(@RequestBody IdAndPage idParam) {
        return mallStoreOrderService.orderList(idParam, getLoginCheck());
    }

    @PostMapping("/delAppOrder")
    @ApiOperation(value = "删除订单")
    public HttpResult<Boolean> delAppOrder(@RequestBody ReqIdParam param) {
        return mallStoreOrderService.delAppOrder(param);
    }


    /****
     * 将用户状态以及订单状态改为取消支付
     * @param param
     * @return
     */
    @PostMapping("/cancelPayOrder")
    @ApiOperation(value = "取消订单（取消支付订单）")
    public HttpResult<Boolean> cancelPayOrder(@RequestBody ReqIdParam param) {
        return mallStoreOrderService.cancelPayOrder(param);
    }


    /**
     * 订单详情
     *
     * @param idParam
     * @Author wangbingcheng
     * @Date 2021年09月13日
     * @Return
     */
    @PostMapping("/order_info")
    @ApiOperation(value = "订单详情")
    public HttpResult<ResOrderGoodsVO> orderInfo(@RequestBody ReqIdParam idParam) {
        return mallStoreOrderService.orderInfo(idParam, getLoginCheck());
    }

    /**
     * 订单详情
     *
     * @Author wangbingcheng
     * @Date 2021年09月30日
     * @Return
     */
    @PostMapping("/confirm_order")
    @ApiOperation(value = "确认订单")
    public HttpResult<Boolean> confirmOrder(@RequestBody ReqIdParam idParam) {
        return mallStoreOrderService.confirmOrder(idParam, getLoginCheck());
    }





    /**
     * 优惠券列表
     *
     * @return
     */
    @PostMapping("/couponUserList")
    @ApiOperation(value = "优惠券列表")
    public HttpResult<List<MallCouponUser>> couponUserList(@RequestBody List<ResOrderMallGoodsVO> goods) {
        return HttpResult.ok(mallCouponService.couponUserList(goods, getUuid()));
    }

    @ApiOperation(value = "店铺商品详情")
    @PostMapping("/detail")
    @ApiIgnore
    public HttpResult<ResStoreGoodsDetailVO> list(@RequestBody ReqIdParam id) {
        return HttpResult.ok(mallStoreGoodsService.queryInfoByGoodsSn(id, getLoginCheck()));
    }

//    //@PostMapping("/queryCommentPage")
//    @ApiOperation(value = "查询商品评价分页列表 common参数需传递商品的商品id")
//    public HttpResult<IPage<ResMallCommentVO>> queryCommentPage(@RequestBody ReqStringListParam param) {
//        return HttpResult.ok(mallCommentService.queryPage(param));
//    }

    @PostMapping("/checkMoney")
    @ApiOperation(value = "验证金额")
    @ApiIgnore
    public HttpResult<ResCheckMoneyVO> checkMoney(@Valid @RequestBody OrderMallSubmitDTO order) {
        return HttpResult.ok(mallStoreOrderService.checkMoney(order, getLoginCheck()));
    }



    /**
     * 删除订单
     */
    @ApiOperation(value = "删除订单")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(mallStoreOrderService.deleteMallOrderByIds(ids));
    }

    @GetMapping("/order_count")
    @ApiOperation(value = "订单数量")
    public HttpResult<ResOrderCountVO> orderCountByOrderSn(){
        return mallStoreOrderService.orderCount(SecurityUtils.getLoginUser());
    }
}
